package com.ht.shop.controller;

import com.ht.shop.annotations.RequestMapping;
import com.ht.shop.pojo.TbOrder;
import com.ht.shop.pojo.TbUser;
import com.ht.shop.service.OrderListService;
import com.ht.shop.service.impl.OrderListServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.rmi.server.UID;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderListController {
    private OrderListService orderListService = new OrderListServiceImpl();
    @Autowired
    public void setOrderListService(OrderListService orderListService) {
        this.orderListService = orderListService;
    }

    @RequestMapping("/detail")
    public String viewDetail(HttpServletRequest request, HttpServletResponse response) {
        TbUser loginUser = (TbUser) request.getSession().getAttribute("loginUser");
        Long uid = loginUser.getId();
        try {
            List<TbOrder> allListById = orderListService.findAllListById(uid);
//            System.out.println("========="+allListById);
//            System.out.println(allListById.get(0).getId());
            request.getSession().setAttribute("ordersList",allListById);
        } catch (Exception e) {
            e.printStackTrace();
        }


        return "forward:/orderList.jsp";
    }
    @RequestMapping("/delete")
    public String deleteOrderList(HttpServletRequest request,HttpServletResponse response){
//        TbUser loginUser = (TbUser) request.getSession().getAttribute("loginUser");
//        Long uid = loginUser.getId();
//        try {
//            List<TbOrder> allListById = orderListDao.findAllListById(id);
//            orderListDao.deleteById(allListById.get(0).getId());
//            request.getSession().setAttribute("ordersList", allListById);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        try {
//            List<TbOrder> allListById = orderListService.findAllListById();
//            request.getSession().setAttribute("ordersList",allListById);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

        String id = request.getParameter("id");//要删除的 id
        orderListService.deleteById(id);

        try {
            List<TbOrder> ordersList = (List<TbOrder>) request.getSession().getAttribute("ordersList");
            ordersList.clear();
            List<TbOrder> allListById1 = orderListService.findAllListById();

            ordersList.addAll(allListById1);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "forward:/orderList.jsp";
    }
}
